package com.cyq.blog.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.LocaleResolver;

/**
 * LocaleResolver class
 *
 * @author chenyeqing
 * @date 2021/5/14
 */
@Configuration
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        // 获取请求中携带的国际化参数
        String lang = request.getParameter("lang");

        // 获取默认的
        Locale requestLocale = Locale.getDefault();

        // 如果有国际化参数，就使用携带的参数
        if (!ObjectUtils.isEmpty(lang)) {
            String[] s = lang.split("_");
            requestLocale = new Locale(s[0], s[1]);
        }
        return requestLocale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
